﻿/*!
 * Copyright 2008-2011 the original author or authors.
 * Licensed under the Apache License, Version 2.0
 */
  
/**
 * 项目监控页面：
 * 第一个tab显示项目台账，左边是一个部门树；
 * 第二个tab显示功能导航图
 * 第三个tab显示图文附件
 * 
 * @author TonyTan
 * @version 1.0, 2012-11-10
 */

Jxstar.currentPage = function(define, myProjectId) {
	//通过我关注的项目打开此功能时，才会有项目ID
	var pageParam = null;
	if (myProjectId != null) {
		pageParam = {};
		pageParam.pageType = 'editgrid';
		pageParam.whereSql = 'project_id = ?';
		pageParam.whereValue = myProjectId;
		pageParam.whereType = 'string';
		pageParam.isQuery = '1';//设置它，表示打开监控界面后还可以继续查询
	}
	//这个导航图ID是通过设计器设计的。
	var graphId = 'jxstar0431';
	var curProjectId = '';
	
	//在project_done功能的表格点击事件中触发执行；
	var showButtonData = function(projectId) {
		//给全局变量赋值
		curProjectId = projectId;
	};
	
	//创建项目相关文档面板
	var createProjectDoc = function(tabPanel) {
		var getForm = function(funId, fileTitle, fileField) {
			var def = Jxstar.findNode(funId);
			var form = new Ext.form.FormPanel({
				layout:'form', width:500, height:40, frame:true,labelWidth:160, 
				items:[
					{xtype:'hidden', fieldLabel:'主键', name:def.pkcol, anchor:'100%'},
					{xtype:'hidden', fieldLabel:'记录状态', name:def.auditcol, anchor:'100%'},
					{xtype:'fileuploadfield', fieldLabel:fileTitle, name:fileField, anchor:'100%'}
				]
			});
			form.funid = funId;
			form.fileField = fileField;
			form.formNode = {define: def};
			return form;
		};
	
		//添加附件字段
		
		tabPanel.add(getForm('project_apply', '项目需求（立项）申请资料', 'project_store__project_file'));
		tabPanel.add(getForm('pur_apply_file', '招标文件', 'pur_apply_file__pur_file'));
		tabPanel.add(getForm('contract_say', '合同谈判记录', 'contract_say__say_file'));
		tabPanel.add(getForm('contract_creat', '合同文本', 'contract__contract_file'));
		tabPanel.add(getForm('project_check', '验收单', 'project_check__check_file'));
		
		tabPanel.doLayout();
	};
	//显示项目文档
	var showProjectDoc = function(tabPanel, projectId) {
		var items = tabPanel.items;
		var funIds = '', fileFields = '';
		for(var i = 0, n = items.getCount(); i < n; i++) {
			var form = items.get(i);
			funIds += form.funid + ';';
			fileFields += form.fileField + ';';
		}
		
		//数据格式为：{funid:'', keyid:'', filename:''}
		var hdCall = function(data) {
			//把没有记录的控件标示为disable
			for(var i = 0, n = items.getCount(); i < n; i++) {
				var form = items.get(i);
				//先清空原来的数据
				form.getForm().set(form.fileField, '');
				var pkcol = form.formNode.define.pkcol;
				form.getForm().set(pkcol, '');
				
				var has = false;
				if (!Ext.isEmpty(data)) {
					for (var j = 0, m = data.length; j < m; j++) {
						var funid = data[j].funid;
						if (form.funid == funid) {
							has = true;
							break;
						}
					}
				}
				form.setDisabled(!has);
			}
		
			if (Ext.isEmpty(data)) {
				JxHint.alert('这些附件字段对应的功能中都没有记录，不能操作附件！');
				return;
			}
			
			//给form赋值
			for (var i = 0, n = data.length; i < n; i++) {
				var funid = data[i].funid;
				var keyid = data[i].keyid;
				var audit = data[i].audit;
				var filename = data[i].filename;
				
				for(var j = 0, m = items.getCount(); j < m; j++) {
					var form = items.get(j);
					if (funid == form.funid) {
						form.getForm().set(form.fileField, filename);
						var def = form.formNode.define;
						form.getForm().set(def.pkcol, keyid);
						//如果真实记录状态为1且有附件名称，则为1，如果没有附件名称，则为0；
						//如果真实记录状态为0，则为0.
						if (audit != '0' && audit != '6') {
							if (filename.length > 0) {
								audit = '1';
							} else {
								audit = '0';
							}
						} else {
							audit = '0';
						}
						form.getForm().set(def.auditcol, audit);
						break;
					}
				}
			}
		};
		
		//根据项目ID到各功能的数据表中查找是否有相关附件
		//约定这些功能的项目ID字段为：project_id
		var params = 'funid=project_done&eventcode=queryfile&pagetype=editgrid';
			params += '&projectId='+ projectId+'&fileFields='+fileFields+'&funIds='+funIds;
		Request.dataRequest(params, hdCall);
	};
	
	//1、创建带树的项目台账面板
	var panelProject = new Ext.Panel({
		border:false,
		layout:'border',
		items:[{
			autoScroll:true,
			region:'west',
			layout:'fit',
			width:160,
			minSize: 160,
	        maxSize: 300,
			split:true,
			border:false
		},{
			region:'center',
			layout:'fit',
			border:false
		}]
	});
	Jxstar.createTree('project_query', panelProject);
	//项目台账表格所在的面板
	var parentGrid = panelProject.getComponent(1);
	//处理项目进度按钮的方法作为面板的成员方法，方便在项目进度数据加载时执行
	parentGrid.showButtonData = showButtonData;
	Jxstar.createPage('project_done', 'gridpage', parentGrid, pageParam);

	//3、构建主Tab面板
	var tabGridForm = new Ext.TabPanel({
		border:false,
		closeAction:'close',
		activeTab:0,
		items:[{
			pagetype:'grid',
			title: '项目基本信息',
			autoScroll:true,
			layout:'fit',
			border:false,
			iconCls:'tab_grid',
			items:[panelProject]
		},{
			pagetype:'pro_mon',
			title: '项目导航中心',
			autoScroll:true,
			layout:'fit',
			border:false,
			iconCls:'tab_grid'
		},{
			pagetype:'pro_doc',
			title: '项目资料存档',
			autoScroll:true,
			layout:'form',
			border:true,
			bodyStyle: 'padding: 20px 10px 0 10px;',
			iconCls:'tab_grid'
		},{
			pagetype:'pro_form',
			title: '项目信息',
			autoScroll:true,
			layout:'fit',
			border:false,
			iconCls:'tab_form'
		},{
			pagetype:'plan_form',
			title: '投资计划',
			autoScroll:true,
			layout:'fit',
			border:false,
			iconCls:'tab_form'
		}]
	});
	//添加项目文档
	//Jxstar.createPage('project_attach', 'gridpage', tabGridForm.getComponent(2));
	createProjectDoc(tabGridForm.getComponent(2));
	//添加项目信息
	Jxstar.createPage('project_done', 'formpage', tabGridForm.getComponent(3));
	//添加投资计划
	Jxstar.createPage('money_query', 'formpage', tabGridForm.getComponent(4));
	//处理项目信息表单数据
	tabGridForm.on('beforetabchange', function(tabPanel, newTab, currentTab){
		var pagetype = newTab.pagetype;
		if (pagetype != 'grid') {
			var curProjectId = '';
			var grid = parentGrid.getComponent(0);
			var records = JxUtil.getSelectRows(grid);
			if (records.length > 0) {
				curProjectId = records[0].get('project_base__project_id');
			}
			if (curProjectId.length == 0) {
				JxHint.alert('请选择一条项目记录！');
				return false;
			}
		}
	});
	tabGridForm.on('tabchange', function(tabPanel, activeTab){
		var pagetype = activeTab.pagetype;
		if (pagetype == 'grid') return;
		var curPage = activeTab.getComponent(0);
		
		//显示项目监控
		if (pagetype == 'pro_mon') {
			//显示功能导航图，并标记有数据的节点
			JxWfGraph.createGraph(activeTab, graphId, {project_id:curProjectId}, true);
		}
		//显示项目文档
		if (pagetype == 'pro_doc') {
			/*var showAttach = function(data) {
				var where_sql = '(fun_id = ? and data_id = ?)';
				if (data.ids.length > 0) {
					where_sql += ' or (sys_attach.attach_id in ('+ data.ids +'))';
				}
				var where_type = 'string;string';
				var where_value = 'project_query;'+curProjectId;
				
				//设置目标功能信息
				curPage.attachDataId = curProjectId;
				curPage.attachFunId = 'project_query';
				curPage.attachAudit = '1';
			
				Jxstar.loadData(curPage, {where_sql:where_sql, where_value:where_value, where_type:where_type});
			};
		
			var params = 'projectid=' + curProjectId + '&funid=project_query';
			params += '&pagetype=grid&eventcode=pattach';

			//发送请求，取相关附件ID
			Request.postRequest(params, showAttach);*/
			
			showProjectDoc(activeTab, curProjectId);
		}
		//显示项目信息
		if (pagetype == 'pro_form') {
			var form = curPage.getForm();
			var grid = parentGrid.getComponent(0);
			var records = JxUtil.getSelectRows(grid);
			var record = records[0];
			
			form.myGrid = grid;
			form.myStore = grid.getStore();
			form.myRecord = record;
			form.loadRecord(record);
		}
		//加载投资计划数据
		if (pagetype == 'plan_form') {
			var options = {
				where_sql: 'plan_status < ? and project_id = ?',
				where_type: 'string;string',
				where_value: '71;' + curProjectId,
				callback: function(data) {
					if (data.length > 0) {
						var r = curPage.formNode.event.newRecord(data[0]);
						
						curPage.getForm().myRecord = r;
						curPage.getForm().loadRecord(r);
					}
				}
			};
			Jxstar.queryData('money_query', options);
		}
	});

	return tabGridForm;
};